

#include <material/effecteditor/effecttreemodel.h>
#include <qmimedata.h>


cceMaterialEffectTreeModel::cceMaterialEffectTreeModel (cceMaterialManager* mgr)
  : QAbstractItemModel ()
  , _mgr (mgr)
{
  connect (_mgr, SIGNAL(EffectAdded (cceMaterialEffect*)), this, SLOT(ResetContent ()));
  connect (_mgr, SIGNAL(EffectChanged (cceMaterialEffect*)), this, SLOT(ResetContent ()));
  connect (_mgr, SIGNAL(EffectRemoved (cceMaterialEffect*)), this, SLOT(ResetContent ()));

  setSupportedDragActions (Qt::ActionMask);
}

void cceMaterialEffectTreeModel::ResetContent ()
{
  reset ();
}

int cceMaterialEffectTreeModel::columnCount (const QModelIndex&) const
{
  return Column_COUNT;
}

QVariant cceMaterialEffectTreeModel::data (const QModelIndex& index, int role) const
{
  if (!index.isValid ())
    {
      return QVariant ();
    }

  cceMaterialEffect* effect = static_cast<cceMaterialEffect*>(index.internalPointer ());

  if (role == Qt::DisplayRole)
    {
      switch (index.column ())
        {
        case Column_Name:
          return QVariant (effect->GetName ());
        default:
          break;
        }
    }
  return QVariant ();
}

QModelIndex cceMaterialEffectTreeModel::index (int row, int column, const QModelIndex &parent) const
{
  if (parent.isValid ()) return QModelIndex ();
  if (column < 0 || column >= Column_COUNT) return QModelIndex ();
  if (row < 0 || row >= _mgr->GetNumberOfEffects()) return QModelIndex ();
  return createIndex (row, column, _mgr->GetEffect(row));
}

QModelIndex cceMaterialEffectTreeModel::parent (const QModelIndex &index) const
{
  return QModelIndex ();
}

int cceMaterialEffectTreeModel::rowCount (const QModelIndex &parent) const
{
  if (parent.isValid ()) return 0;
  return _mgr->GetNumberOfEffects();
}

Qt::ItemFlags cceMaterialEffectTreeModel::flags (const QModelIndex& index) const
{
  Qt::ItemFlags f = QAbstractItemModel::flags (index);
  if (index.isValid())
    {
      f |= Qt::ItemIsDragEnabled;
    }
  return f;
}

QString cceMaterialEffectTreeModel::GetEffectName (unsigned row) const
{
  if (row < 0 || row >= _mgr->GetNumberOfEffects()) return QString::Null ();
  cceMaterialEffect* effect= _mgr->GetEffect(row);
  if (!effect) return QString::Null ();
  return effect->GetName ();
}


QMimeData* cceMaterialEffectTreeModel::mimeData (const QModelIndexList& indices) const
{
  if (indices.size () == 0)
    {
      return 0;
    }
  const QModelIndex& index = indices[0];
  if (!index.isValid ())
    {
    }

  int row = index.row ();
  foreach (const QModelIndex& idx, indices)
    {
      if (!idx.isValid () || idx.row () != row)
        {
          return 0;
        }
    }

  cceMaterialEffect* effect= static_cast<cceMaterialEffect*>(index.internalPointer ());
  if (!effect)
    {
      return 0;
    }

  QMimeData* mimeData = new QMimeData ();
  mimeData->setData (CCE_MATERIAL_EFFECTNAME_MIME, effect->GetName ().toAscii ());
  return mimeData;
}



QVariant cceMaterialEffectTreeModel::headerData (int section, Qt::Orientation orientation, int role) const
{
  if (section < 0 || section >= Column_COUNT || role != Qt::DisplayRole)
    {
      return QVariant ();
    }
  Q_UNUSED (orientation);
  switch (section)
    {
    case Column_Name:
      return "Name";
    default:
      break;
    }
  return QVariant ();
}

